#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib
import urlparse

def normalize(url, base):
    """
    Normalize an URL: make it absolute and remove
    fragments
    """
    (scheme, netloc, path, query, frag) = urlparse.urlsplit(url)
    if scheme == '':
        #it's relative
        if not path.startswith(urlparse.urlsplit(base).netloc):
        #remove fragment
            nurl = urlparse.urljoin(base, url.split('#')[0])
    else:
        #remove fragment
        nurl = url.split('#')[0]
    return nurl

if __name__ == '__main__':
    
    urls = [
            'http://absoluteurl.com/',
            'http://absolutewithfrag.com/foo,html#frag',
            'http://absolute.com/foo.html',
            'absolute.com/foo.html',
            '/foo.html',
            './foo.html',
            '/../foo.html',
            ]
    
    base = 'http://example.org'

    for url in urls:
        print url
        print "\t->", normalize(url, base)
